package com.sgb.kjwl.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;

import okhttp3.OkHttpClient;

public class TextViewString {

    /**
     * 文字风格
     */
    //粗体
    public static final int BOLD = Typeface.BOLD;
    //斜粗体
    public static final int BOLD_ITALIC = Typeface.BOLD_ITALIC;
    //斜体
    public static final int ITALIC = Typeface.ITALIC;
    //正常
    public static final int NORMAL = Typeface.NORMAL;
//    public static final int DEFAULT_BOLD = Typeface.DEFAULT_BOLD;
//    public static final int DEFAULT = Typeface.DEFAULT;

    /**
     *  文本图片出现位置
     */
    //下
    public static final int ALIGN_BOTTOM = DynamicDrawableSpan.ALIGN_BOTTOM;
    //总线
    public static final int ALIGN_BASELINE = DynamicDrawableSpan.ALIGN_BASELINE;
    private SpannableString spannableString;

    private TextViewString(TextViewString.Builder s){

    }

    public SpannableString getStr(){
        return spannableString;
    }

    public class Builder{
        private int start;
        private int end;
        private String s;

        private Context context;

        public Builder() {

        }

        public Builder(Context context) {
            this.context = context;
        }

        public Builder(String s) {
            this.s = s;
            spannableString = new SpannableString(s);
        }

        public Builder(String s, Context context) {
            this.s = s;
            this.context = context;
            spannableString = new SpannableString(s);
        }

        /**
         * 设置文本颜色
         * @param color 前景色的color值
         * @return
         */
        public TextViewString.Builder foregroundColor(@ColorInt int color){
            spannableString.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder foregroundColor(int start, int end, @ColorInt int color){
            spannableString.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本背景色
         * @param color 背景色的color值
         * @return
         */
        public TextViewString.Builder backgroundColor(@ColorInt int color){
            spannableString.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder backgroundColor(int start, int end, @ColorInt int color){
            spannableString.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 相对文本大小
         * @param size 缩放文本的比例
         * @return
         */
        public TextViewString.Builder relativeSize(float size){
            spannableString.setSpan(new RelativeSizeSpan(size), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder relativeSize(float size, int start, int end){
            spannableString.setSpan(new RelativeSizeSpan(size), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本绝对大小
         * @param px 绝对大小的px值
         * @return
         */
        public TextViewString.Builder absoluteSize(int px){
            spannableString.setSpan(new AbsoluteSizeSpan(px), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder absoluteSize(int px, int start, int end){
            spannableString.setSpan(new AbsoluteSizeSpan(px), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本的绝对大小
         * @param dp 绝对大小的dp值
         * @return
         */
        public TextViewString.Builder absoluteSizeForDp(int dp){
            spannableString.setSpan(new AbsoluteSizeSpan(dp,true), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder absoluteSizeForDp(int dp, int start, int end){
            spannableString.setSpan(new AbsoluteSizeSpan(dp,true), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本中划线
         * @return
         */
        public TextViewString.Builder trikesThrough(){
            spannableString.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder trikesThrough(int start, int end){
            spannableString.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本下划线
         * @return
         */
        public TextViewString.Builder underline(){
            spannableString.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder underline(int start, int end){
            spannableString.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本上标
         * @return
         */
        public TextViewString.Builder superscript(){
            spannableString.setSpan(new SuperscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder superscript(int start, int end){
            spannableString.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 为文本设置下标
         * @return
         */
        public TextViewString.Builder subscript(){
            spannableString.setSpan(new SubscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder subscript(int start, int end){
            spannableString.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本风格(粗体、斜体)
         * @return
         */
        public TextViewString.Builder style(int style){
            spannableString.setSpan(new StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder style(int style, int start, int end){
            spannableString.setSpan(new StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 将文本添加图片
         * @param drawable
         * @return
         */
        public TextViewString.Builder image(@DrawableRes int drawable){
            spannableString.setSpan(new ImageSpan(context,drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder image(@DrawableRes int drawable, int start, int end){
            spannableString.setSpan(new ImageSpan(context,drawable), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 设置文本点击事件
         * @param click 继承ClickableSpan的子类
         * @return
         */
        public TextViewString.Builder click(ClickableSpan click){
            spannableString.setSpan(click, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder click(ClickableSpan click, int start, int end){
            spannableString.setSpan(click, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        /**
         * 为文本设置超链接
         * @param
         * @return
         */
        public TextViewString.Builder url(String url){
            spannableString.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }

        public TextViewString.Builder url(String url, int start, int end){
            spannableString.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return this;
        }
        /**
         * 设置文本内容
         * @param s
         * @return
         */
        public TextViewString.Builder setStr(String s){
            this.s = s;
            spannableString = new SpannableString(s);
            return this;
        }

        /**
         * 设置长度
         * @param start
         * @param end
         * @return
         */
        public TextViewString.Builder startAndEnd(int start, int end){
            this.start = start;
            this.end = end;
            return this;
        }

        public TextViewString.Builder context(Context context){
            this.context = context;
            return this;
        }

        public TextViewString build(){
            return new TextViewString(this);
        }
    }
}
